<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace api\admin\controller;
use cmf\controller\RestAdminBaseController;
use api\mall\model\StoreModel;
/**
 * Description of LoggerController
 *
 * @author PC
 */
class StoreController extends RestAdminBaseController{
    //put your code here
    public function index(){
        
    }
    
    public function save(){
        $post = $this->request->post();
        $post = $this->doSave($post);
        $post['update_time'] = time();
        $model = new StoreModel();
        if(isset($post['id']) && $post['id']>0){
             $model->where('id',$post['id'])->update($post);
        }else{
             $post['create_time'] = time();
             $post['item_status'] = 0;
             $model->insert($post);
        }
        
        $this->success('保存成功');
    }
    

    
    public function status($id,$status){
        $model = new StoreModel();
        $model->where('id',$id)->update(['item_status'=>$status,'update_time'=> time()]);
        $this->success('status');
    }


    /**
     * 显示指定的资源
     *
     * @param  int $id
     */
    public function read($id)
    {
        $this->success('read');
    }

    /**
     * 保存更新的资源
     *
     */
    public function update()
    {
        $post = $this->request->post();
        $id=$post['id'];

        $post = $this->doSave($post);
        $model = new StoreModel();
        $post['update_time'] = time();
        $model->where('id',$id)->update($post);
        $this->success('update');
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     */
    public function delete($id)
    {
        $model = new StoreModel();
        $model->where('id',$id)->delete();
        $this->success('删除成功');
    }
    
    private function doSave($post){
        $validate = [
            'item_name'=>'require|length:2,60',
            'item_desc'=>'require|length:10,60',
            'item_num'=>'require|integer|gt:0',
            'item_type'=>'require|length:1,16',
//            'item_images'=>'require|lenght:2,60',
            'item_price'=>'require|integer|gt:0'
        ];
        $msgs = [
            'item_name.require'=>'商品名称不能为空',
            'item_name.lenght'=>'商品名称长度不符合',
            'item_desc.require'=>'商品描述不能为空',
            'item_name.require'=>'商品名称不能为空'
        ];
        $result = $this->validate($post, $validate,$msgs);
        if($result!==true){
            $this->error($result,$post);
        }
        return $post;
    }
}
